package com.itheima.controller.store;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BeanServlet;
import com.itheima.domain.store.Address;
import com.itheima.domain.store.City;
import com.itheima.domain.store.Company;
import com.itheima.service.store.CompanyService;
import com.itheima.service.store.Impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;
import com.mysql.jdbc.StringUtils;
import org.apache.poi.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @param
 * @return
 */
@WebServlet("/store/company/*")
public class CompanyServlet extends BeanServlet {
    //创建业务层复用
    CompanyService service = new CompanyServiceImpl();
    protected void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从前端页面获取传递过来的id,这里只支持单删，故结果为单个变量
        String id = req.getParameter("id");
        //调用业务层方法
        service.deleteById(id);
        //删除后跳回主页面
        this.findByPage(req,resp);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过工具类获取页面表单提交的数据，参数需要请求对象，封装的实体类对象的.class，还有就是日期格式
        Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
        //调用业务层方法，将封装好的对象作为参数使用
        int i = service.update(company);
        //调用遍历分页方法，再到list.jsp
        this.findByPage(req, resp);
    }
    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //根据id查询企业的方法，用于数据回显
        String id = req.getParameter("id");
        Company company=service.findCompanyById(id);
        //将查询结果存进 域中
        req.setAttribute("company",company);
        //转发到修改页面
        req.getRequestDispatcher("/pages/store/company/update.jsp").forward(req,resp);
    }

    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过工具类获取页面表单提交的数据，参数需要请求对象，封装的实体类对象的.class，还有就是日期格式
        Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
        //调用业务层方法，将封装好的对象作为参数使用
        int i = service.save(company);
        //调用遍历分页方法，再到list.jsp
        this.findByPage(req, resp);
    }

    protected void findAllAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取级联的城市id
        String id = req.getParameter("id");
        List<Address> list = service.findAddressById(Integer.parseInt(id));
        //使用异步对象的方法将集合转成json格式的字符串
        String json = new ObjectMapper().writeValueAsString(list);
        //设置响应的字符集，重点是text/json
        resp.setContentType("text/json;charset=UTF-8");
        resp.getWriter().write(json);
    }

    protected void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置变量
        Integer pageNum = 1;
        Integer pageSize = 5;
        //从前端获取分页参数
        String pageNumStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        if (!StringUtils.isNullOrEmpty(pageNumStr)) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (!StringUtils.isNullOrEmpty(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        List<Company> list = service.findByPage(pageNum, pageSize);//调用业务层方法,获取返回值
        HttpSession session = req.getSession();//获取session域对象
        //调用分页根据对象
        PageInfo<Company> page = new PageInfo<Company>(list);
        session.setAttribute("page", page);//装货
        resp.sendRedirect(req.getContextPath() + "/pages/store/company/list.jsp");//重定向到list页面
    }

    protected void findAllCity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<City> list = service.findAllCity();//调用业务层方法,获取返回值
        //使用异步对象的方法将集合转成json格式的字符串
        String json = new ObjectMapper().writeValueAsString(list);
        //设置响应的字符集，重点是text/json
        resp.setContentType("text/json;charset=UTF-8");
        resp.getWriter().write(json);
    }
}
